package org.apmap.servlets;

import org.apmap.base.Flat;
import org.apmap.base.Main;
import org.apmap.base.Office;
import org.apmap.common.Utils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import javax.servlet.http.Cookie;
import java.util.ArrayList;
import java.util.List;

public class GetHousesServlet {

    public Flat[] getHouses(double swlatDouble, double swlngDouble, double nelatDouble, double nelngDouble) {

        long swlat = (long) (Utils.MULTI * swlatDouble);
        long swlng = (long) (Utils.MULTI * swlngDouble);
        long nelat = (long) (Utils.MULTI * nelatDouble);
        long nelng = (long) (Utils.MULTI * nelngDouble);

        List<Long> favorites = getFavorites("f");

        Session session = Main.getSession();
        Query query;
        if (favorites.isEmpty()) {
            query = session.createQuery("from Flat where latLong >=:swlat and lngLong>=:swlng and latLong <=:nelat and lngLong<=:nelng");
        } else {
            query = session.createQuery("from Flat where (latLong >=:swlat and lngLong>=:swlng and latLong <=:nelat and lngLong<=:nelng) or id in (:favorites)");
        }

        query.setLong("swlat", swlat);
        query.setLong("swlng", swlng);
        query.setLong("nelat", nelat);
        query.setLong("nelng", nelng);
        if (!favorites.isEmpty()) {
            query.setParameterList("favorites", favorites);
        }

        List<org.apmap.base.Flat> list = query.list();

        for (Flat flat : list) {
            flat.setUserid(flat.getUser().getId());
        }

        session.close();

        return list.toArray(new Flat[list.size()]);
    }

    private List<Long> getFavorites(String startWith) {
        WebContext webContext = WebContextFactory.get();

        Cookie[] cookies = webContext.getHttpServletRequest().getCookies();
        for (Cookie cooky : cookies) {
            System.out.println("cooky.getName() = " + cooky.getName());
        }

        List<Long> favorites = new ArrayList<Long>();
        for (Cookie cookie : cookies) {
            if (cookie.getName().startsWith(startWith)) {
                try {
                    favorites.add(Long.parseLong(cookie.getName().substring(1)));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }
        return favorites;
    }

    public Office[] getOffices(double swlatDouble, double swlngDouble, double nelatDouble, double nelngDouble) {
        long swlat = (long) (Utils.MULTI * swlatDouble);
        long swlng = (long) (Utils.MULTI * swlngDouble);
        long nelat = (long) (Utils.MULTI * nelatDouble);
        long nelng = (long) (Utils.MULTI * nelngDouble);

        List<Long> favorites = getFavorites("o");

        Session session = Main.getSession();
        Query query;
        if (favorites.isEmpty()) {
            query = session.createQuery("from Office where latLong >=:swlat and lngLong>=:swlng and latLong <=:nelat and lngLong<=:nelng");
        } else {
            query = session.createQuery("from Office where (latLong >=:swlat and lngLong>=:swlng and latLong <=:nelat and lngLong<=:nelng) or id in (:favorites)");
        }

        query.setLong("swlat", swlat);
        query.setLong("swlng", swlng);
        query.setLong("nelat", nelat);
        query.setLong("nelng", nelng);
        if (!favorites.isEmpty()) {
            query.setParameterList("favorites", favorites);
        }

        List<org.apmap.base.Office> list = query.list();

        for (Office office : list) {
            office.setUserid(office.getUser().getId());
        }

        session.close();

        return list.toArray(new Office[list.size()]);
    }



    /*public static void main(String[] args) throws Exception {
        javax.xml.bind.JAXBContext jaxbContext = javax.xml.bind.JAXBContext.newInstance(Points.class);
        Flat flat = new Flat();
        flat.setAddress("ADR 1");
        flat.setLat(100.0);
        flat.setLng(200.0);

        Flat flat2 = new Flat();
        flat2.setAddress("ADR 2");
        flat2.setLat(300.0);
        flat2.setLng(400.0);

        Points points = new Points();
        points.setFlatList(Arrays.asList(flat, flat2));

        javax.xml.bind.Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(points, System.out);

        String str = "<points><flatList id=\"3\" address=\"ADR 333\" lat=\"10.0\" lng=\"20.0\"/></points>";
        javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        points = (Points) unmarshaller.unmarshal(new ByteArrayInputStream(str.getBytes()));
        System.out.println("points = " + points);
        flat = points.getFlatList().get(0);
        System.out.println("flat = " + flat.getLat());
        System.out.println("flat = " + flat.getLng());


    } */
}
